package com.grj.order.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.grj.common.model.Order;
import com.grj.order.repository.OrderRepository;
import com.grj.order.service.OrderProducer;
import com.grj.order.service.OrderWriteService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author ganru
 * @date 2025/10/19 1:19
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderRepository repository;
    private final OrderProducer producer;
    private final OrderWriteService orderWriteService;

    public OrderController(OrderRepository repository, OrderProducer producer, OrderWriteService orderWriteService) {
        this.repository = repository;
        this.producer = producer;
        this.orderWriteService = orderWriteService;
    }

    @PostMapping("/create")
    public String createOrder(@RequestParam String userId, @RequestParam double amount) throws JsonProcessingException {
        Order order = new Order();
        order.setOrderId(UUID.randomUUID().toString());
        order.setUserId(userId);
        order.setAmount(amount);
        order.setStatus("CREATED");
//        repository.save(order);
        orderWriteService.createOrder(order);

        producer.sendOrderCreated(order);
        return "Order created: " + order.getOrderId();
    }
}